#include "ObjectManager.h"
#include "../GameObjects/BaseInterface.h"
#include "../GameObjects/BaseObject.h"

void ObjectManager::ShutDownObjectManager()
{
	RemoveAllObjects();
	RemoveAllStaticObjects();
}

/* DYNAMIC OBJECTS */

bool ObjectManager::AddObject(BaseInterface* _object)
{
	if(!_object)
		return false;

	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = objectVec.begin(); objectIter != objectVec.end(); ++objectIter)
	{
		if((*objectIter) == _object)
			return false;
	}

	objectVec.push_back(_object);

	return true;
}

void ObjectManager::UpdateObjects(float _dT)
{
	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = objectVec.begin(); objectIter != objectVec.end(); ++objectIter)
	{
		(*objectIter)->Update(_dT);
	}
}

// Render ALL objects necessary
void ObjectManager::RenderObjects()
{
	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = objectVec.begin(); objectIter != objectVec.end(); ++objectIter)
	{
		(*objectIter)->Render();
	}

	for(objectIter = staticObjectVec.begin(); objectIter != staticObjectVec.end(); ++objectIter)
	{
		(*objectIter)->Render();
	}
}

bool ObjectManager::RemoveObject(BaseInterface* _object)
{
	if(!_object)
		return false;

	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = objectVec.begin(); objectIter != objectVec.end(); ++objectIter)
	{
		if((*objectIter)== _object)
		{
			_object->Release();
			objectVec.erase(objectIter);

			return true; 
		}
	}

	return false;
}

void ObjectManager::RemoveAllObjects()
{
	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = objectVec.begin(); objectIter != objectVec.end(); ++objectIter)
	{
		(*objectIter)->Release();
	}

	objectVec.clear();
}

/* STATIC OBJECTS */

bool ObjectManager::AddStaticObject(BaseInterface* _object)
{
	if(!_object)
		return false;

	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = staticObjectVec.begin(); objectIter != staticObjectVec.end(); ++objectIter)
	{
		if((*objectIter) == _object)
			return false;
	}

	staticObjectVec.push_back(_object);

	return true;
}

bool ObjectManager::RemoveStaticObject(BaseInterface* _object)
{
	if(!_object)
		return false;

	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = staticObjectVec.begin(); objectIter != staticObjectVec.end(); ++objectIter)
	{
		if((*objectIter)== _object)
		{
			_object->Release();
			staticObjectVec.erase(objectIter);

			return true; 
		}
	}

	return false;
}

void ObjectManager::RemoveAllStaticObjects()
{
	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = staticObjectVec.begin(); objectIter != staticObjectVec.end(); ++objectIter)
	{
		(*objectIter)->Release();
	}

	staticObjectVec.clear();
}

void ObjectManager::CheckCollision()
{
	vector<BaseInterface*>::iterator objectIterA;
	vector<BaseInterface*>::iterator objectIterB;

	for(objectIterA = objectVec.begin(); objectIterA != objectVec.end(); ++objectIterA)
	{
		for (objectIterB=objectVec.begin() ; objectIterB != objectVec.end() ; objectIterB++)
		{
			// If iterators at same place dont compare
			if(objectIterA==objectIterB)
				continue;

			// Insert type checking code here if you want same object types not to collide (ie player to player)
			if((*objectIterA)->GetType() == (*objectIterB)->GetType())
				continue;

			BaseObject* objectA = (BaseObject*)(*objectIterA);
			BaseObject* objectB = (BaseObject*)(*objectIterB);
			
			//if(objectA->GetType() == OBJ_PLAYER && objB->GetType() == OBJ_ENEMY)
			//		break;

			float distX = objectA->GetPositionX() - objectB->GetPositionX();
			distX *= distX;

			float distY = objectA->GetPositionY() - objectB->GetPositionY();
			distY *= distY;

			if((distX + distY) < 250000.0f)
			{
				if((*objectIterA)->CheckCollision((*objectIterB)))
					break;
			}
		}
	}
}

void ObjectManager::CheckActive()
{
	vector<BaseInterface*>::iterator objectIter;

	for(objectIter = objectVec.begin(); objectIter != objectVec.end(); ++objectIter)
	{
		if(!(*objectIter)->IsActive())
		{
			//if((*objectIter)->GetType() == OBJ_ENEMY)
			//{
			//	(*objectIter)->Release();
			//	objectIter = objectVec.erase(objectIter);
			//	continue;
			//}
		}
	}
}